package acl.hibernate.dao;


import acl.hibernate.po.OaDepartments;
import hibernate.HibernateBaseDAO;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Departmentdao extends HibernateBaseDAO{
	// 实例化一个session工厂
	// ----------------------------------------------------------
	
	//SessionFactory factory = config.buildSessionFactory();

	// ----------------------------------------------------------
	public List tree(Integer id) {
		// 根据部门id，获取部门信息
		// Author:Silence
		session = this.getSession();
		String hql = "From OaDepartments as department WHERE department.id=:id";
		List tree = null;
		try {
			Query q = session.createQuery(hql);
			q.setInteger("id", id);
			tree = q.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			this.closeSession(session);
		}
		return tree;
	}

	// ----------------------------------------------------------
	// ----------------------------------------------------------
	public List treeBypid(Integer pid) {
		// 根据父部门id，获取子部门信息
		// Author:Silence
		session = this.getSession();
		String hql = "From OaDepartments as department WHERE department.parentId=:pid";
		List tree = null;
		try {
			Query q = session.createQuery(hql);
			q.setInteger("pid", pid);
			tree = q.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			this.closeSession(session);
		}
		return tree;
	}
	
	// ----------------------------------------------------------
	public List getAllBypid(Integer pid) {
		// 根据父部门id，获取当前部门及所有子部门部门信息
		// Author:Never
		session = this.getSession();
		String hql = "From OaDepartments as department WHERE department.parentId=:pid or department.id=:pid";
		List tree = null;
		try {
			Query q = session.createQuery(hql);
			q.setInteger("pid", pid);
			tree = q.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			this.closeSession(session);
		}
		return tree;
	}
	public List getValidDepartment(){
		//获取有效（未删除）部门
		List result=null;
		session = this.getSession();
		String hql = "From OaDepartments as department WHERE department.departmentState=1";
		
		try {
			Query q = session.createQuery(hql);
			result=q.list();			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			this.closeSession(session);
		}
		return result;
	}
	public List getAllDepartment() {
		// 获取所有部门信息		
		return this.getList(OaDepartments.class.getName());
	}

	// ----------------------------------------------------------
	public void creatNewDepartment(OaDepartments department)throws Exception {
		// 新建节点
		// Author:Silence
		session = this.getSession();
		Transaction tr = null;		
		try{
			tr=session.beginTransaction();
			session.save(department);
			tr.commit();
		}catch (Exception e){
			if(tr!=null)
				tr.rollback();
			e.printStackTrace();
		}
		finally{
			this.closeSession(session);
		}
	}
//--------------------------------------------------------------------
	public void deleteDepartments(Integer pid){
		//删除节点
		//Author:Silence
		//time:09-3-18
		List all = this.getAllBypid(pid);
		session = this.getSession();
		Transaction tr = null;
		try{
			tr=session.beginTransaction();
			for(int i = 0;i<all.size();i++){					
				//String hql = "DELETE OaDepartments WHERE id =:id";
				//使用删除标记删除部门
				String hql="update OaDepartments set departmentState=0 where id=:id ";
				Query q = session.createQuery(hql);
				q.setInteger("id",((OaDepartments)all.get(i)).getId());
				q.executeUpdate();								
			}
			tr.commit();
		}catch(Exception e){
			if(tr!=null)
				tr.rollback();
			e.printStackTrace();
		}	
		finally{
			this.closeSession(session);
		}
	}
	public boolean deleteDepartment(String[] idArray)
	{		
		if(idArray!=null||idArray.length>0)
		{
			session = this.getSession();			
			Transaction tr = null;
			try{
				tr=session.beginTransaction();
				for(int i=0;i<idArray.length;i++){
					OaDepartments department=(OaDepartments) session.get(OaDepartments.class.getName(), Integer.parseInt(idArray[i]));
					//session.delete(department);
					//使用删除标志
					department.setDepartmentState((new Integer(0)).byteValue());
					session.save(department);
				}
				tr.commit();
			}catch(Exception e){
				if(tr!=null)
					tr.rollback();
				e.printStackTrace();
				this.closeSession(session);
				return false;
			}
		}		
		return true;
	}
	//--------------------------------------------------------------------
	public void reNameDepartment(Integer id,String name,String number){
		//节点重命名
		//Author:Silence
		//time:09-3-18
		session = this.getSession();
		OaDepartments d = (OaDepartments)this.tree(id).get(0);
		//d.setId(id);
		d.setYxsmc(name);
		d.setYxsh(number);		
		Transaction tr = null;
		try{
			tr=this.session.beginTransaction();
			session.update(d);
			tr.commit();
		}catch(Exception e){
			if(tr!=null)
				tr.rollback();
			e.printStackTrace();				
		}finally{
			this.closeSession(session);		
		}
	}
	//--------------------------------------------------------------------
	public void reWriteContent(Integer id,String content){
		//修改部门介绍
		//Author:Silence
		//time:09-3-21
		session = this.getSession();
		OaDepartments d = (OaDepartments)this.tree(id).get(0);
		Date date = new Date();
		d.setModified(date);
		d.setContent(content);
		d.setId(id);
		d.setChild(Byte.valueOf("0"));		
		Transaction tr = null;
		try{
			tr=this.session.beginTransaction();
			session.update(d);
			tr.commit();
		}catch(Exception e){
			if(tr!=null)
				tr.rollback();
			e.printStackTrace();			
		}finally{
			this.closeSession(session);
		}
		
	}
	public boolean saveDepartment(OaDepartments department)
	{
		session = this.getSession();
		Transaction tr = null;
		try
		{
			tr=session.beginTransaction();
			session.saveOrUpdate(department);
			tr.commit();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
